package Year22_Month06.day01;

import java.util.*;

public class 二叉树的层序遍历2_BFS {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        List<List<Integer>> list  = new ArrayList<>();
        if(root != null){
            queue.add(root);
        }
        while(!queue.isEmpty()){
            int count = queue.size();
            List<Integer> mylis = new ArrayList<>();
            while(count > 0){
                TreeNode node = queue.poll();
                mylis.add(node.val);
                if(node.left != null){
                    queue.add(node.left);
                }
                if(node.right != null){
                    queue.add(node.right);
                }
                count--;
            }
            list.add(0,mylis);
        }
        return list;
    }
}
